﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class control_chitiettour_InfoChiTietTour : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadSoNguoi();
            LoadHangVe();
            LoadHangBay();
            LoadComboDd();
            LoadComboKh(); 
        }
    }
    protected string GetNameDiaDiemById(long id)
    {
        DataTable dt = DataAccess.ExeSQLToDataTable("select Name from DiaDanh where IdYahoo = '" + id + "'");
        if (dt.Rows.Count == 0)
        {
            return "";
        }
        else
        {
            return dt.Rows[0][0] == null ? "" : dt.Rows[0][0].ToString();
        }

    }
    protected string GetInfoDatTour()
    {
        string htmlCode = "";
        long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
        //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
        DataTable tbl =
            DataAccess.ExeSQLToDataTable("select * from dev_news where idnews = '" + idTour + "' order by idnews desc");

        if (tbl.Rows.Count > 0)
        {
            htmlCode = @"<div class='btn_Xemchitiet'>
                            <div class='style_like_face'>
                                <div class='fb-like' data-href='http://lanhdaitourist.com/chitiettourdulich.aspx?id=" + tbl.Rows[0]["idnews"] + @"' data-send='true' data-layout='button_count'
                                    data-show-faces='true'>
                                </div>
                            </div>
                        </div>";
        }

        return htmlCode;
    }

    protected void LoadSoNguoi()
    {
        List<ListItem> items = new List<ListItem>();
        for (int i = 0; i < 10; i++)
        {
            items.Add(new ListItem(i.ToString()));
        }
        selectNguoiLon.Items.AddRange(items.ToArray());
        selectNguoiLon.SelectedIndex = 0;
        selectTreEm.Items.AddRange(items.ToArray());
        selectTreEm.SelectedIndex = 0;
        selectEmBe.Items.AddRange(items.ToArray());
        selectEmBe.SelectedIndex = 0;
    }
    protected void LoadHangVe()
    {
        List<ListItem> items = new List<ListItem>();
        items.Add(new ListItem("Phổ thông"));
        items.Add(new ListItem("Thương gia"));
        items.Add(new ListItem("Khuyến mại"));
        selectHangVe.Items.AddRange(items.ToArray());
        selectHangVe.SelectedIndex = 0;
    }
    protected void LoadHangBay()
    {
        DataTable dt =
            DataAccess.ExeSQLToDataTable("select * from HangBay");
        if (dt.Rows.Count > 0)
        {
            selectHangBay.DataSource = dt;
            selectHangBay.DataTextField = "Name";
            selectHangBay.DataValueField = "Id";
            selectHangBay.DataBind();
        }
    }
    protected void LoadComboKh()
    {
        DataTable dt = DataAccess.ExeSQLToDataTable("select * from DiaDanh");
        if (dt.Rows.Count > 0)
        {
            List<ListItem> list = new List<ListItem>();
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(new ListItem(dr["Name"].ToString().Replace("Tour ", "")));
            }
            selectKh.Items.AddRange(list.ToArray());
            selectKh.SelectedIndex = 0;
        }
    }
    
    protected void LoadComboDd()
    {
        DataTable dt = DataAccess.ExeSQLToDataTable("select * from DiaDanh");
        if (dt.Rows.Count > 0)
        {
            List<ListItem> list = new List<ListItem>();
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(new ListItem(dr["Name"].ToString().Replace("Tour ", "")));
            }
            selectDd.Items.AddRange(list.ToArray());
            selectDd.SelectedIndex = 0;
        }
    }
    protected void btnDatTourClick(object sender, EventArgs e)
    {
        ccJoin.ValidateCaptcha(txtXacNhan.Value);
        string hoten = HoTen.Value;
        if (ccJoin.UserValidated && CheckInfo())
        {
            long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
            //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
            string form = "lanhdaitourist.hy@gmail.com";
            string add = "lanhdaitourist.hy@gmail.com";
            string loaive = rbSearch.SelectedItem.Value;
            if (loaive=="1")
            {
                loaive = "Khứ hồi";
            }
            else
            {
                loaive = "Một chiều";
            }
            string subject = "Tôi muốn đặt vé máy bay! Vui lòng liên hệ với tôi";
            string boby = "Tôi là : " + HoTen.Value + " | " +
                          "Số di động : " + DiDong.Value + " | " +
                          "Email : " + Email.Value + " | " +
                          "Yêu cầu khác : " + YeuCau.Value + " | " +
                          "Loại vé (Khứ hồi/Một chiều) : " + loaive + " | " +
                          "Nơi đi : " + selectKh.SelectedItem.Text + " | " +
                          "Nơi đến : " + selectDd.SelectedItem.Text + " | " + 
                          "Ngày đi : " + txtDate.Text+ " | " + 
                          "Ngày về : " + txtDateVe.Text + " | " + 
                          "Số người lớn : " + selectNguoiLon.SelectedItem.Text +" | " +
                          "Trẻ em : " + selectTreEm.SelectedItem.Text + " | " +
                          "Em bé : " + selectEmBe.SelectedItem.Text + " | " +
                          "Hãng bay : " + selectHangBay.SelectedItem.Text + " | " + 
                          "Hạng vé : " + selectHangVe.SelectedItem.Text;
            string displayname = "Khách hàng đặt vé";
            string servermail = "smtp.gmail.com";
            int port = 587;
            string user = "lanhdaitourist.hy@gmail.com";
            string pass = "0975573383";
            bool ssl = true;
            bool value = ITNLip.SendMail(form, add, subject, boby, displayname, servermail, port, user, pass, ssl);
            if (value)
            {
                Label1.Text = "Đặt tour thành công! ";
            }
            else
            {
                Label1.Text = "Đặt tour thất bại. Liên hệ trực tiếp với chúng tôi!";
            }

        }
        else
        {
            if (!ccJoin.UserValidated)
            {
                if (!CheckInfo())
                {
                    return;
                }
                Label1.Text = "Mã bảo vệ chưa chính xác";
                return;
            }

        }
    }
    protected bool CheckInfo()
    {
        bool value = false;
        if (txtDate.Text == "")
        {
            Label1.Text = "Nhập ngày đi";
            txtDate.Focus();
            return false;
        }
        if (HoTen.Value == "")
        {
            Label1.Text = "Nhập họ tên người đặt vé";
            HoTen.Focus();
            return false;
        }
        if (Email.Value == "")
        {
            Label1.Text = "Nhập email người đặt vé";
            Email.Focus();
            return false;
        }
        if (DiDong.Value == "")
        {
            Label1.Text = "Nhập số di động người đặt vé.";
            DiDong.Focus();
            return false;
        }
        else
        {
            return true;
        }

    }
    protected string GetNameTourDuLich()
    {
        string htmlCode = "";
        long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
        //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
        DataTable tbl =
            DataAccess.ExeSQLToDataTable("select * from dev_news where checktour=1 and status = 1 and idnews = '" + idTour + "' order by idnews desc");
        if (tbl.Rows.Count > 0)
        {
            htmlCode += ITNLip.CatChuoi(tbl.Rows[0]["title"].ToString(), 60);
        }
        return htmlCode;
    }
    protected string GetInfoSketTour()
    {
        string htmlCode = "";
        long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
        //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
        DataTable tbl = DataAccess.ExeSQLToDataTable("select * from dev_news where checktour=1 and idnews = '" + idTour + "' order by idnews desc");

        if (tbl.Rows.Count > 0)
        {
            htmlCode += @"<div class='img-daidien_tour'>
                        <img src='files_upload/images/pictour/" + tbl.Rows[0]["picture"] + @"' />
                    </div>
                    <div class='noidung_tour_short'>
                        <div class='name_tour'>
                            <a href='#'><b>" + ITNLip.CatChuoi(tbl.Rows[0]["title"].ToString(), 80) + @"</b></a>
                        </div>
                        <div class='list_info_tour'>
                            <ul>
                                <li>
                                    <b>Xuất phát: </b><a>" + GetNameDiaDiemById(Convert.ToInt64((tbl.Rows[0]["diemxuatphat"] == null ? "0" : tbl.Rows[0]["diemxuatphat"]))) + @"</a>
                                </li>
                                <li>
                                    <b>Điểm đến: </b><a>" + GetNameDiaDiemById(Convert.ToInt64((tbl.Rows[0]["diemden"] == null ? "0" : tbl.Rows[0]["diemden"]))) + @"</a>
                                </li>
                                 <li>
                                    <b>Hãng bay: </b><a>" + GetNameHangBayById(tbl.Rows[0]["hangbay"] == null ? 0 : Convert.ToInt64(tbl.Rows[0]["hangbay"])) + @"</a>
                                </li>
                                <li>
                                    <b>Giá vé: </b><a class='giakm'>" + (String.Format("{0:0,0}", tbl.Rows[0]["giatour"])) + @" VNĐ</a>
                                </li>
                               <li>
                                    <b>Khứ hồi: </b><a>" + tbl.Rows[0]["khuhoi"] + @"</a>
                                </li>
                            </ul>
                        </div>
                    </div>";
        }
        return htmlCode;
    }
    protected string GetNameHangBayById(long id)
    {
        DataTable dt = DataAccess.ExeSQLToDataTable("select Name from HangBay where Id = '" + id + "'");
        if (dt.Rows.Count == 0)
        {
            return "";
        }
        else
        {
            return dt.Rows[0][0] == null ? "" : dt.Rows[0][0].ToString();
        }

    }
    protected string GetInfoPriceTourInclude()
    {
        string htmlCode = "";
        long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
        //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
        DataTable tbl =
            DataAccess.ExeSQLToDataTable("select * from dev_news where idnews = '" + idTour + "' order by idnews desc");
        if (tbl.Rows.Count > 0)
        {
            htmlCode += tbl.Rows[0]["body"];
        }
        return htmlCode;
    }
    protected string GetInfoPriceTourExclude()
    {
        string htmlCode = "";
        long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
        //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
        DataTable tbl =
            DataAccess.ExeSQLToDataTable("select * from dev_news where idnews = '" + idTour + "' order by idnews desc");
        if (tbl.Rows.Count > 0)
        {
            htmlCode += tbl.Rows[0]["body"];
        }
        return htmlCode;
    }
    protected string GetInfoContent()
    {
        string htmlCode = "";
        long idTour = Convert.ToInt64(Request.QueryString.Get("id"));
        //string link_web = "?menu=" + (idMenu == 0 ? 161 : idMenu) + "&parent=" + (idParent == 0 ? 0 : idParent) + "";
        DataTable tbl =
            DataAccess.ExeSQLToDataTable("select * from dev_news where idnews = '" + idTour + "' order by idnews desc");
        if (tbl.Rows.Count > 0)
        {
            htmlCode += tbl.Rows[0]["body"];
        }
        return htmlCode;
    }
    protected string LoadInfoThoigian(long id)
    {
        string result = "";
        DataTable list =
           DataAccess.ExeSQLToDataTable("select Name from Ngay where IdDuLich=" + id + "");
        if (list.Rows.Count > 0)
        {
            result = list.Rows[0][0].ToString();
        }
        return result;
    }
}